{% set pypi_name = 'ec2-api' %}
{% set source = fetch_source('https://tarballs.openstack.org/ec2-api/ec2-api-master.tar.gz') %}
{% set upstream_version = upstream_version() %}
{% set rpm_release = '1' %}
%global with_doc 1
%if 0%{?rhel} || 0%{?fedora}
%global rdo 1
%endif
Name:           {{ py2name() }}
Version:        {{ py2rpmversion() }}
Release:        {{ py2rpmrelease() }}
Epoch:          {{ epoch('ec2-api') }}
Summary:        OpenStack EC2 API
License:        {{ license('Apache-2.0') }}
URL:            https://docs.openstack.org/ec2-api
Source0:        {{ source|basename }}
Source1:        openstack-ec2-api.logrotate
Source2:        openstack-ec2-api.tmpfiles
Source3:        openstack-ec2-api.defaultconf
Source20:       openstack-ec2-api-api.service
Source21:       openstack-ec2-api-metadata.service
Source22:       openstack-ec2-api-s3.service
Source50:       openstack-ec2-api.README.config
BuildRequires:  openstack-macros
BuildRequires:  {{ py3('botocore') }}
BuildRequires:  {{ py3('python-cinderclient') }}
BuildRequires:  {{ py3('python-glanceclient') }}
BuildRequires:  {{ py3('httplib2') }}
BuildRequires:  {{ py3('keystoneauth1') }}
BuildRequires:  {{ py3('mock') }}
BuildRequires:  {{ py3('python-neutronclient') }}
BuildRequires:  {{ py3('python-novaclient') }}
BuildRequires:  {{ py3('oslo.cache') }}
BuildRequires:  {{ py3('oslo.concurrency') }}
BuildRequires:  {{ py3('oslo.config') }}
BuildRequires:  {{ py3('oslo.db') }}
BuildRequires:  {{ py3('oslo.service') }}
BuildRequires:  {{ py3('oslotest') }}
BuildRequires:  {{ py3('pbr') }}
BuildRequires:  {{ py3('stestr') }}
Requires:       logrotate
Requires:       python3-{{ pypi_name }} = %{epoch}:%{version}-%{release}
BuildArch:      noarch
%if 0%{?suse_version}
BuildRequires:  systemd-rpm-macros
Requires(pre):  pwdutils
%{?systemd_requires}
%else
BuildRequires:  systemd
Requires(post): systemd
Requires(postun): systemd
Requires(pre):  shadow-utils
Requires(preun): systemd
%endif

%description
AWS EC2 and VPC API support in standalone service for OpenStack.
At first uses nova's EC2 API for non-VPC functionality.

%package -n python3-{{ pypi_name }}
Summary:        OpenStack EC2 API
Group:          Development/Languages/Python
Requires:       {{ py3('Babel') }}
Requires:       {{ py3('Paste') }}
Requires:       {{ py3('PasteDeploy') }}
Requires:       {{ py3('Routes') }}
Requires:       {{ py3('SQLAlchemy') }}
Requires:       {{ py3('WebOb') }}
Requires:       {{ py3('botocore') }}
Requires:       {{ py3('python-cinderclient') }}
Requires:       {{ py3('cryptography') }}
Requires:       {{ py3('eventlet') }}
Requires:       {{ py3('python-glanceclient') }}
Requires:       {{ py3('greenlet') }}
Requires:       {{ py3('httplib2') }}
Requires:       {{ py3('keystoneauth1') }}
Requires:       {{ py3('python-keystoneclient') }}
Requires:       {{ py3('lxml') }}
Requires:       {{ py3('python-neutronclient') }}
Requires:       {{ py3('python-novaclient') }}
Requires:       {{ py3('python-openstackclient') }}
Requires:       {{ py3('oslo.cache') }}
Requires:       {{ py3('oslo.concurrency') }}
Requires:       {{ py3('oslo.config') }}
Requires:       {{ py3('oslo.context') }}
Requires:       {{ py3('oslo.db') }}
Requires:       {{ py3('oslo.log') }}
Requires:       {{ py3('oslo.serialization') }}
Requires:       {{ py3('oslo.service') }}
Requires:       {{ py3('oslo.utils') }}
Requires:       {{ py3('six') }}
Requires:       {{ py3('sqlalchemy-migrate') }}

%description -n python3-{{ pypi_name }}
AWS EC2 and VPC API support in standalone service for OpenStack.
At first uses nova's EC2 API for non-VPC functionality.

This package contains the Python library.

%if 0%{?with_doc}
%package doc
Summary:        OpenStack EC2 API - Documentation
Group:          Documentation/HTML
BuildRequires:  {{ py3('Sphinx') }}
BuildRequires:  {{ py3('openstackdocstheme') }}

%description doc
AWS EC2 and VPC API support in standalone service for OpenStack.
At first uses nova's EC2 API for non-VPC functionality.

This package contains the documentation.
%endif

%package api
Summary:        OpenStack EC2 API - API
Group:          Development/Languages/Python
Requires:       %{name} = %{epoch}:%{version}-%{release}

%description api
AWS EC2 and VPC API support in standalone service for OpenStack.
At first uses nova's EC2 API for non-VPC functionality.

This package contains the API server.

%package metadata
Summary:        OpenStack EC2 API - Metadata
Group:          Development/Languages/Python
Requires:       %{name} = %{epoch}:%{version}-%{release}

%description metadata
AWS EC2 and VPC API support in standalone service for OpenStack.
At first uses nova's EC2 API for non-VPC functionality.

This package contains the metadata server.

%package s3
Summary:        OpenStack EC2 API - S3
Group:          Development/Languages/Python
Requires:       %{name} = %{epoch}:%{version}-%{release}

%description s3
AWS EC2 and VPC API support in standalone service for OpenStack.
At first uses nova's EC2 API for non-VPC functionality.

This package contains the S3 server.

%prep
%autosetup -p1 -n {{ pypi_name }}-{{ upstream_version }}
%py_req_cleanup

%build
%{py3_build}

# build documentation
%if 0%{?with_doc}
PBR_VERSION={{ upstream_version }} %sphinx_build --keep-going -b html doc/source doc/build/html
rm -rf doc/build/html/.{doctrees,buildinfo}
%endif

# generate the sample config files
PYTHONPATH=. oslo-config-generator --config-file=etc/ec2api/ec2api-config-generator.conf

%install
%{py3_install}

# Setup directories
install -d -m 750 %{buildroot}%{_localstatedir}/{lib,log,cache}/ec2-api
install -d -m 755 %{buildroot}%{_sysconfdir}/ec2api
install -d -m 755 %{buildroot}%{_sysconfdir}/ec2api/ec2api.conf.d

# Install logrotate
install -p -D -m 644 %{SOURCE1} %{buildroot}%{_sysconfdir}/logrotate.d/openstack-ec2-api

# install tmpfiles
install -D -m 644 %{SOURCE2} %{buildroot}/%_tmpfilesdir/%name.conf

# install config files
install -p -D -m 640 %{SOURCE3} %{buildroot}%{_sysconfdir}/ec2api/ec2api.conf.d/010-ec2api.conf
install -p -D -m 640 etc/ec2api/ec2api.conf.sample %{buildroot}%{_sysconfdir}/ec2api/ec2api.conf
install -p -D -m 640 etc/ec2api/api-paste.ini %{buildroot}%{_sysconfdir}/ec2api/
install -p -D -m 640 %{SOURCE50} %{buildroot}%{_sysconfdir}/ec2api/README.config

# install systemd service files
install -p -D -m 644 %{SOURCE20} %{buildroot}%{_unitdir}/openstack-ec2-api-api.service
install -p -D -m 644 %{SOURCE21} %{buildroot}%{_unitdir}/openstack-ec2-api-metadata.service
install -p -D -m 644 %{SOURCE22} %{buildroot}%{_unitdir}/openstack-ec2-api-s3.service

# install symlinks on SUSE
%if 0%{?suse_version}
mkdir -p %{buildroot}%{_sbindir}
ln -s %{_sbindir}/service %{buildroot}%{_sbindir}/rc%{name}-api
ln -s %{_sbindir}/service %{buildroot}%{_sbindir}/rc%{name}-metadata
ln -s %{_sbindir}/service %{buildroot}%{_sbindir}/rc%{name}-s3
%endif

%pre
%openstack_pre_user_group_create ec2-api ec2-api /sbin/nologin
exit 0

%post
%tmpfiles_create %{_tmpfilesdir}/%{name}.conf


%post api
%systemd_post %{name}-api.service

%preun api
%systemd_preun %{name}-api.service

%postun api
%systemd_postun_with_restart %{name}-api.service

%post metadata
%systemd_post %{name}-metadata.service

%preun metadata
%systemd_preun %{name}-metadata.service

%postun metadata
%systemd_postun_with_restart %{name}-metadata.service

%post s3
%systemd_post %{name}-s3.service

%preun s3
%systemd_preun %{name}-s3.service

%postun s3
%systemd_postun_with_restart %{name}-s3.service

%check
rm -v ec2api/tests/unit/test_hacking.py
%{__python3} -m stestr.cli --test-path ec2api/tests/unit run

%files
%license LICENSE
%dir %attr(0750, root, ec2-api) %{_sysconfdir}/ec2api
%dir %attr(0750, root, ec2-api) %{_sysconfdir}/ec2api/ec2api.conf.d/
%{_sysconfdir}/ec2api/README.config
%config(noreplace) %attr(-, root, ec2-api) %{_sysconfdir}/ec2api/ec2api.conf
%config(noreplace) %attr(-, root, ec2-api) %{_sysconfdir}/ec2api/ec2api.conf.d/010-ec2api.conf
%config(noreplace) %attr(-, root, ec2-api) %{_sysconfdir}/ec2api/api-paste.ini
%config(noreplace) %{_sysconfdir}/logrotate.d/openstack-ec2-api
%_tmpfilesdir/%name.conf
%dir %attr(0750, ec2-api, root) %{_localstatedir}/log/ec2-api
%dir %attr(0750, ec2-api, root) %{_localstatedir}/lib/ec2-api
%{_bindir}/ec2-api-manage

%files -n python3-ec2-api
%license LICENSE
%{python3_sitelib}/ec2api*
%{python3_sitelib}/ec2_api-*.egg-info

%if 0%{?with_doc}
%files doc
%license LICENSE
%doc doc/build/html
%endif

%files api
%license LICENSE
%{_bindir}/ec2-api
%{_unitdir}/openstack-ec2-api-api.service
%if 0%{?suse_version}
%{_sbindir}/rc%{name}-api
%endif

%files metadata
%license LICENSE
%{_bindir}/{{ pypi_name }}-metadata
%{_unitdir}/openstack-ec2-api-metadata.service
%if 0%{?suse_version}
%{_sbindir}/rc%{name}-metadata
%endif

%files s3
%license LICENSE
%{_bindir}/{{ pypi_name }}-s3
%{_unitdir}/openstack-ec2-api-s3.service
%if 0%{?suse_version}
%{_sbindir}/rc%{name}-s3
%endif

%changelog

